package com.shanzmoo.merchant.manage.api.manager.product;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.module.product.domain.dto.CategorySaveDto;
import com.shanzmoo.db.module.product.domain.dto.CategoryUpdateDto;
import com.shanzmoo.db.module.product.domain.vo.CategoryVO;
import com.shanzmoo.db.module.product.entity.ProductCategoryEntity;
import com.shanzmoo.db.module.product.service.IProductCategoryService;
import com.shanzmoo.merchant.manage.api.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类manager
 * @author xueqz
 */
@Component
@RequiredArgsConstructor
public class ProductCategoryManager {

    @Autowired
    private IProductCategoryService productCategoryService;

    public SHttpResponse<List<CategoryVO>> getCategoryTree() {
        List<ProductCategoryEntity> productCategoryEntityList = productCategoryService.list(new QueryWrapper<ProductCategoryEntity>()
                .eq("deleted", false));
        List<CategoryVO> categoryTree = getCategoryTree(productCategoryEntityList, 0,"顶级分类");
        return SHttpResponse.ok(categoryTree);
    }

    public SHttpResponse<Boolean> delCatById(Integer catId) {
        List<ProductCategoryEntity> list = productCategoryService.list(new QueryWrapper<ProductCategoryEntity>().eq("deleted", false).eq("parent_id", catId));
        Assert.isTrue(CollUtil.isNotEmpty(list),"该分类下有未删除的子分类时,不允许删除夫分类");
        ProductCategoryEntity entity = new ProductCategoryEntity();
        entity.setId(catId);
        entity.setDeleted(Boolean.TRUE);
        entity.setUpdateUser(SecurityUtil.getCurrentUserId());
        productCategoryService.updateById(entity);
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> addCat(CategorySaveDto categorySaveDto) {
        ProductCategoryEntity convert = categorySaveDto.convert(ProductCategoryEntity.class);
        convert.setCreateUser(SecurityUtil.getCurrentUserId());
        productCategoryService.save(convert);
        return SHttpResponse.ok();
    }

    public SHttpResponse<Boolean> editCat(CategoryUpdateDto categoryUpdateDto) {
        ProductCategoryEntity convert = categoryUpdateDto.convert(ProductCategoryEntity.class);
        convert.setUpdateUser(SecurityUtil.getCurrentUserId());
        productCategoryService.updateById(convert);
        return SHttpResponse.ok();
    }

    private List<CategoryVO> getCategoryTree(List<ProductCategoryEntity> productCategoryEntityList,Integer parentId,String parentName){
        List<CategoryVO> CategoryVOList = new ArrayList<CategoryVO>();
        for (ProductCategoryEntity productCategoryEntity : productCategoryEntityList) {
            if(parentId.equals(productCategoryEntity.getParentId())){
                CategoryVO vo = productCategoryEntity.convert(CategoryVO.class);
                vo.setParentName(parentName);
                CategoryVOList.add(vo);
            }
        }
        for (CategoryVO CategoryVO : CategoryVOList) {
            List<CategoryVO> categoryTree = getCategoryTree(productCategoryEntityList, CategoryVO.getId(),CategoryVO.getName());
            if(categoryTree != null && categoryTree.size() > 0){
                CategoryVO.setChildren(categoryTree);
            }
        }
        return CategoryVOList;
    }
}
